/*******************************************************************************
 * Copyright (c) 2014-8-18 @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a>.
 * All rights reserved.
 *
 * Contributors:
 *     <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> - initial API and implementation
 ******************************************************************************/
package com.foreveross.project.projectonline.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.iff.infra.util.NumberHelper;
import org.iff.infra.util.StringHelper;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
 * @since 2014-8-18
 */
@SuppressWarnings("unchecked")
@Named("userVerifyService")
public class UserVerifyService {

	/**
	 * {DATE,DAYS,PAGE_NUM,PAGE_SIZE}
	 * @param conditions
	 * @return
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
	 * @since 2014-10-9
	 */
	public Page<Record> findNeedVerifyUser(Map<String, Object> conditions) {
		Date date = (Date) conditions.get("DATE");
		int days = NumberHelper.getInt(conditions.get("DAYS"), 1);
		date = date == null ? new Date() : date;
		date = DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
		days = days == 0 ? 1 : (Math.abs(days) > 30 ? 30 * days / Math.abs(days) : days);// keep days between -30,30
		String select = " select tu.* ";
		String sql = " from TP_USER tu inner join TP_DYNAMIC_PROPERTY tdp on tu.ID=tdp.TARGET_ID and tdp.CATEGORY='USER' and tdp.TYPE='USER_VERIFY' and tdp.CODE='USER_VERIFY' and tdp.NAME='ID_NO' and tdp.IS_ENABLE='Y' and tdp.UPDATE_TIME>=? and tdp.UPDATE_TIME<=? "
				+ " left join TP_DYNAMIC_PROPERTY tdp2 on tu.ID=tdp2.TARGET_ID and tdp2.CATEGORY='USER' and tdp2.TYPE='USER_VERIFY' and tdp2.CODE='USER_VERIFY' and tdp2.NAME='IS_VERIFY' and tdp2.IS_ENABLE='Y' and tdp2.UPDATE_TIME>=? and tdp2.UPDATE_TIME<=? "
				+ " where tu.IS_ACTIVE='Y' and tu.IS_ENABLE='Y' and tdp2 is null ";
		Date min = days > 0 ? date : DateUtils.addDays(date, days);
		Date max = days < 0 ? date : DateUtils.addDays(date, days);
		Page<Record> page = Db.paginate(NumberHelper.getInt(conditions.get("PAGE_NUM"), 1), NumberHelper.getInt(
				conditions.get("PAGE_SIZE"), 10), select, sql, min, max, min, max);
		return page;
	}

	public Map<String, Object> getUserVerify(String userId) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (StringUtils.isBlank(userId)) {
			return result;
		}
		List<Record> list = Db
				.find(
						"select NAME,VALUE from TP_DYNAMIC_PROPERTY where TARGET_ID=? and CATEGORY='USER' and TYPE='USER_VERIFY' and CODE='USER_VERIFY' and IS_ENABLE='Y'",
						userId);
		for (Record r : list) {
			result.put(r.getStr("NAME"), r.get("VALUE"));
		}
		return result;
	}

	/**
	 * {DATE,DAYS,PAGE_NUM,PAGE_SIZE}
	 * @param conditions
	 * @return
	 * @author <a href="mailto:iffiff1@hotmail.com">Tyler Chen</a> 
	 * @since 2014-10-9
	 */
	public Page<Record> findNeedVerifyCompany(Map<String, Object> conditions) {
		Date date = (Date) conditions.get("DATE");
		int days = NumberHelper.getInt(conditions.get("DAYS"), 1);
		date = date == null ? new Date() : date;
		date = DateUtils.truncate(date, Calendar.DAY_OF_MONTH);
		days = days == 0 ? 1 : (Math.abs(days) > 30 ? 30 * days / Math.abs(days) : days);// keep days between -30,30
		String select = " select tu.* ";
		String sql = " from TP_USER tu inner join TP_DYNAMIC_PROPERTY tdp on tu.ID=tdp.TARGET_ID and tdp.CATEGORY='USER' and tdp.TYPE='COMPANY_VERIFY' and tdp.CODE='COMPANY_VERIFY' and tdp.NAME='REGISTER_CAPITAL' and tdp.IS_ENABLE='Y' and tdp.UPDATE_TIME>=? and tdp.UPDATE_TIME<=? "
				+ " left join TP_DYNAMIC_PROPERTY tdp2 on tu.ID=tdp2.TARGET_ID and tdp2.CATEGORY='USER' and tdp2.TYPE='COMPANY_VERIFY' and tdp2.CODE='COMPANY_VERIFY' and tdp2.NAME='IS_VERIFY' and tdp2.IS_ENABLE='Y' and tdp2.UPDATE_TIME>=? and tdp2.UPDATE_TIME<=? "
				+ " where tu.IS_ACTIVE='Y' and tu.IS_ENABLE='Y' and tdp2 is null ";
		Date min = days > 0 ? date : DateUtils.addDays(date, days);
		Date max = days < 0 ? date : DateUtils.addDays(date, days);
		Page<Record> page = Db.paginate(NumberHelper.getInt(conditions.get("PAGE_NUM"), 1), NumberHelper.getInt(
				conditions.get("PAGE_SIZE"), 10), select, sql, min, max, min, max);
		return page;
	}

	public Map<String, Object> getCompanyVerify(String userId) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (StringUtils.isBlank(userId)) {
			return result;
		}
		List<Record> list = Db
				.find(
						"select NAME,VALUE from TP_DYNAMIC_PROPERTY where TARGET_ID=? and CATEGORY='USER' and TYPE='COMPANY_VERIFY' and CODE='COMPANY_VERIFY' and IS_ENABLE='Y'",
						userId);
		for (Record r : list) {
			result.put(r.getStr("NAME"), r.get("VALUE"));
		}
		return result;
	}

	public boolean verifyUser(String userId) {
		//INSERT INTO `tp_dynamic_property` VALUES ('0431a25e91264a89bae35cdde440f5f1', 'USER', 'USER_VERIFY', '116d96edf62d4780959df634a5a67b38', 'IS_VERIFY', 'IS_VERIFY', 'Y', '0', '', '2014-08-26 12:51:13', '2014-08-26 12:51:13', 'Y');
		if (StringUtils.isBlank(userId)) {
			return false;
		}
		Number number = Db.queryNumber("select count(1) from TP_USER where ID=? and IS_ENABLE='Y'", userId);
		if (number == null || number.intValue() < 1) {
			return false;
		}
		try {
			Db.save("TP_DYNAMIC_PROPERTY", new Record()//
					.set("ID", StringHelper.uuid())//
					.set("CATEGORY", "USER")//
					.set("TYPE", "USER_VERIFY")//
					.set("TARGET_ID", userId)//
					.set("NAME", "IS_VERIFY")//
					.set("CODE", "IS_VERIFY")//
					.set("VALUE", "Y")//
					.set("SORT", 0)//
					.set("DESCIPTION", "")//
					.set("CREATE_TIME", new Date())//
					.set("UPDATE_TIME", new Date())//
					.set("IS_ENABLE", "Y")//
					);
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	public boolean verifyCompany(String userId) {
		//INSERT INTO `tp_dynamic_property` VALUES ('0431a25e91264a89bae35cdde440f5f1', 'USER', 'COMPANY_VERIFY', '116d96edf62d4780959df634a5a67b38', 'IS_VERIFY', 'IS_VERIFY', 'Y', '0', '', '2014-08-26 12:51:13', '2014-08-26 12:51:13', 'Y');
		if (StringUtils.isBlank(userId)) {
			return false;
		}
		Number number = Db.queryNumber("select count(1) from TP_USER where ID=? and IS_ENABLE='Y'", userId);
		if (number == null || number.intValue() < 1) {
			return false;
		}
		try {
			Db.save("TP_DYNAMIC_PROPERTY", new Record()//
					.set("ID", StringHelper.uuid())//
					.set("CATEGORY", "USER")//
					.set("TYPE", "COMPANY_VERIFY")//
					.set("TARGET_ID", userId)//
					.set("NAME", "IS_VERIFY")//
					.set("CODE", "IS_VERIFY")//
					.set("VALUE", "Y")//
					.set("SORT", 0)//
					.set("DESCIPTION", "")//
					.set("CREATE_TIME", new Date())//
					.set("UPDATE_TIME", new Date())//
					.set("IS_ENABLE", "Y")//
					);
		} catch (Exception e) {
			return false;
		}
		return true;
	}
}
